#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Zug = 1;
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::paintEvent(QPaintEvent *)
{
    //public QRect Feld[8][8];
    int i,j;
    QChar c[8] = { 'A','B','C','D','E','F','G','H' };
    QChar z[8] = { '8','7','6','5','4','3','2','1' };
    QPainter painter(this);
    const int size = 80;
    QPen paintpen(Qt::black);
    paintpen.setWidth(5);
    painter.setPen(paintpen);

    for(i=1;i<9;i++)
    {
        painter.drawText(40, size*i+40, z[i-1]);
        painter.drawText(745, size*i+40, z[i-1]);
        for(j=1;j<9;j++)
        {
            Feld[i-1][j-1] = QRect(i*size,j*size,size,size);
            painter.drawText(size*j+40, 60, c[j-1]);
            painter.drawText(size*j+40, 745, c[j-1]);
            if((i%2)==0)
                if((j%2)!=0)
                    painter.fillRect(i*size, j*size, size, size, Qt::darkGray);
                else
                   painter.fillRect(i*size, j*size, size, size, Qt::white);
            else
                if((j%2)!=0)
                    painter.fillRect(i*size, j*size, size, size, Qt::white);
                else
                   painter.fillRect(i*size, j*size, size, size, Qt::darkGray);
            if(FeldFig[i-1][j-1].possible)
                painter.fillRect(i*size, j*size, size, size, QColor(0,255,0,100));
            if(FeldFig[i-1][j-1].dead)
                painter.fillRect(i*size, j*size, size, size, QColor(255,0,0,100));

       if(FeldFig[i-1][j-1].color==1)
       {
            switch(FeldFig[i-1][j-1].art)
            {
                case 1:
                    painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/pawn_white.svg"));
                    break;
                case 2:
                    painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/rook_white.svg"));
                    break;
                case 3:
                    painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/bishop_white.svg"));
                    break;
                case 4:
                    painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/knight_white.svg"));
                    break;
                case 5:
                    painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/king_white.svg"));
                    break;
                case 6:
                    painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/queen_white.svg"));
                    break;
            }
       }
       else
       {
           switch(FeldFig[i-1][j-1].art)
           {
               case 1:
                   painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/pawn_black.svg"));
                   break;
               case 2:
                   painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/rook_black.svg"));
                   break;
               case 3:
                   painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/bishop_black.svg"));
                   break;
               case 4:
                   painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/knight_black.svg"));
                   break;
               case 5:
                   painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/king_black.svg"));
                   break;
               case 6:
                   painter.drawPixmap(Feld[i-1][j-1], QPixmap(":/res/img/queen_black.svg"));
                   break;
           }

       }
        }
    }
    paintpen.setWidth(2);
    painter.setPen(paintpen);
    painter.drawRect(size,size,size*8,size*8);

}


void MainWindow::mousePressEvent(QMouseEvent *event)
{
    QString X,Y,X1,Y2,Z;


    if (event->buttons() & Qt::LeftButton)
    {
        startx = event ->pos().x();
        starty = event ->pos().y();
        for(int i=0;i <= 7;i++){
            for(int j = 0; j<8;j++){
                FeldFig[i][j].possible = false;
                FeldFig[i][j].dead = false;
            }
        }

        startx = (startx - 80) / 80 +1;
        starty = (starty - 80) / 80 +1;

        if((Zug == FeldFig[startx-1][starty-1].color))
        {
            Zug_meoglich(FeldFig[startx-1][starty-1].art , startx-1, starty-1);
            repaint();
        }
    }
    else
    {
        if(event->buttons() & Qt::RightButton){
            endx = event ->pos().x();
            endy = event ->pos().y();

            endx = (endx - 80) / 80 +1;
            endy = (endy - 80) / 80 +1;
            if((startx == endx) && (starty == endy))
            {
            }
            else
            {

                if(Zug == FeldFig[startx-1][starty-1].color)
                {
                    if(((FeldFig[endx-1][endy-1].possible == true) || (FeldFig[endx-1][endy-1].dead == true)))
                    {
                        FeldFig[endx-1][endy-1].art = FeldFig[startx-1][starty-1].art;
                        FeldFig[endx-1][endy-1].color = FeldFig[startx-1][starty-1].color;
                        FeldFig[startx-1][starty-1].art = 0;
                        FeldFig[startx-1][starty-1].color = 0;
                    }
                    for(int i=0;i <= 7;i++){
                        for(int j = 0; j<8;j++){
                            FeldFig[i][j].possible = false;
                        }
                    }
                    repaint();
                    if(Zug)
                         Z = QString("%1: %2%3 -> %4%5").arg("     Weiß").arg(UmwandelnX(startx)).arg(UmwandelnY(starty)).arg(UmwandelnX(endx)).arg(UmwandelnY(endy));
                    else
                         Z = QString("%1: %2%3 -> %4%5").arg("Schwarz").arg(UmwandelnX(startx)).arg(UmwandelnY(starty)).arg(UmwandelnX(endx)).arg(UmwandelnY(endy));

                    ui->listWidget->addItem(Z);
                    Zug_meoglich(FeldFig[endx-1][endy-1].art , endx-1, endy-1);
                    repaint();
                    if(KoenigMatt(Zug))
                    {

                        if(Zug)
                            ui->lSchach->setText(QString("Schwarz ist\nim Schach!!"));
                        else
                            ui->lSchach->setText(QString("Weiß ist\nim Schach!"));
                    }
                    Zug = !Zug;
                 }
            }

        }
       }
    repaint();

    return;
}

char MainWindow::UmwandelnX(int zahl)
{
   switch(zahl)
   {
    case 1:
        return 'A';
    case 2:
        return 'B';
    case 3:
        return 'C';
    case 4:
        return 'D';
    case 5:
        return 'E';
    case 6:
        return 'F';
    case 7:
        return 'G';
    case 8:
        return 'H';
    default:
        return 0;
   }
}

int MainWindow::UmwandelnY(int zahl)
{
    switch(zahl)
    {
     case 1:
         return 8;
     case 2:
         return 7;
     case 3:
         return 6;
     case 4:
         return 5;
     case 5:
         return 4;
     case 6:
         return 3;
     case 7:
         return 2;
     case 8:
         return 1;
     default :
         return 0;
    }
}

bool Intervall(int x, int y)
{
    const int unten = 0;
    const int oben =  7;
    if((x >= unten) && (x <= oben) && (y >= unten) && (y <= oben))
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool MainWindow::Gegner(int color, int fx, int fy)
{
    if(!Intervall(fx,fy))
        return 0;
    if((FeldFig[fx][fy].color != color) && (FeldFig[fx][fy].art != 0))
        return 1;
    else
        return 0;
}

void MainWindow::Zug_meoglich(int Figur, int x, int y)
{
    int i;
    switch(Figur)
    {
    case 1:
    {
        if(FeldFig[x][y].color == 0)//Bauer, 0 = Schwarz
        {
            if(FeldFig[x][1].art == 1)
            {
                for(i= 1;i <= 2; i++)
                {
                    if(FeldFig[x][y+i].art == 0)
                    {
                        FeldFig[x][y+i].possible = true;
                    }
                    else
                    {
                        FeldFig[x][y+i].possible = false;
                        break;
                    }
                }
           }
           else
                if (FeldFig[x][y+1].art == 0)
                {
                    FeldFig[x][y+1].possible = true;
                }
            if(Gegner(0, x+1, y+1))
                FeldFig[x+1][y+1].dead = true;
            if(Gegner(0, x-1, y+1))
                FeldFig[x-1][y+1].dead = true;
        }
        else
        {
            if(FeldFig[x][6].art == 1)
            {
                for(int i= 1;i <= 2; i++)
                {
                    if(FeldFig[x][y-i].art == 0)
                    {
                        FeldFig[x][y-i].possible = true;
                    }
                    else
                    {
                        FeldFig[x][y-i].possible = false;
                        break;
                    }
                }
           }
           else
           {
                if (FeldFig[x][y-1].art == 0)
                {
                    FeldFig[x][y-1].possible = true;
                }
           }
            if(Gegner(1, x+1, y-1))
                FeldFig[x+1][y-1].dead = true;
            if(Gegner(1, x-1, y-1))
                FeldFig[x-1][y-1].dead = true;
        }
    }
        break;
    case 2:
    {
        i = 1;
        while(FeldFig[x][y+i].art == 0)
        {
            FeldFig[x][y+i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x, y+i))
        {
            FeldFig[x][y+i].dead = true;
        }

        i = 1;
        while((FeldFig[x+i][y].art == 0) && (Intervall(x+i,y)))
        {
            FeldFig[x+i][y].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x+i, y))
        {
            FeldFig[x+i][y].dead = true;
        }

        i = 1;
        while(FeldFig[x][y-i].art == 0)
        {
            FeldFig[x][y-i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x, y-i))
        {
            FeldFig[x][y-i].dead = true;
        }

        i = 1;
        while(FeldFig[x-i][y].art == 0)
        {
            FeldFig[x-i][y].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x-i, y))
        {
            FeldFig[x-i][y].dead = true;
        }

    }
        break; //Turm
    case 3:
    {
        i = 1;
        while((FeldFig[x+i][y+i].art == 0)&& (Intervall(x+i,y+i)))
        {
            FeldFig[x+i][y+i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x+i, y+i))
        {
            FeldFig[x+i][y+i].dead = true;
        }

        i = 1;
        while((FeldFig[x-i][y+i].art == 0)&& (Intervall(x-i,y+i)))
        {
            FeldFig[x-i][y+i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x-i, y+i))
        {
            FeldFig[x-i][y+i].dead = true;
        }

        i = 1;
        while((FeldFig[x+i][y-i].art == 0)&& (Intervall(x+i,y-i)))
        {
            FeldFig[x+i][y-i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x+i, y-i))
        {
            FeldFig[x+i][y-i].dead = true;
        }
        i = 1;
        while((FeldFig[x-i][y-i].art == 0)&& (Intervall(x-i,y-i)))
        {
            FeldFig[x-i][y-i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x-i, y-i))
        {
            FeldFig[x-i][y-i].dead = true;
        }
    }
        break; //Laufer
    case 4: // Springer
    {
        if(Intervall(x+1,y+2) && (FeldFig[x+1][y+2].art == 0))
            FeldFig[x+1][y+2].possible = true;

        if(Intervall(x-1,y+2)&& (FeldFig[x-1][y+2].art == 0))
         FeldFig[x-1][y+2].possible = true;

        if((Intervall(x+2,y+1) && (FeldFig[x+2][y+1].art == 0)))
            FeldFig[x+2][y+1].possible = true;

        if((Intervall(x-2,y+1)&& (FeldFig[x-2][y+1].art == 0)))
            FeldFig[x-2][y+1].possible = true;

        if((Intervall(x-2,y-1)&& (FeldFig[x-2][y-1].art == 0)))
            FeldFig[x-2][y-1].possible = true;

        if((Intervall(x+2,y-1)&& (FeldFig[x+2][y-1].art == 0)))
            FeldFig[x+2][y-1].possible = true;

        if((Intervall(x-1,y-2)&& (FeldFig[x-1][y-2].art == 0)))
        FeldFig[x-1][y-2].possible = true;

        if((Intervall(x+1,y-2)&& (FeldFig[x+1][y-2].art == 0)))
            FeldFig[x+1][y-2].possible = true;


        if(Gegner(FeldFig[x][y].color, x+1, y+2))
            FeldFig[x+1][y+2].dead = true;

        if(Gegner(FeldFig[x][y].color, x-1, y+2))
         FeldFig[x-1][y+2].dead = true;

        if(Gegner(FeldFig[x][y].color, x+2, y+1))
            FeldFig[x+2][y+1].dead = true;

        if(Gegner(FeldFig[x][y].color, x-2, y+1))
            FeldFig[x-2][y+1].dead = true;

        if(Gegner(FeldFig[x][y].color, x-2, y-1))
            FeldFig[x-2][y-1].dead = true;

        if(Gegner(FeldFig[x][y].color, x+2, y-1))
            FeldFig[x+2][y-1].dead = true;

        if(Gegner(FeldFig[x][y].color, x-1, y-2))
        FeldFig[x-1][y-2].dead = true;

        if(Gegner(FeldFig[x][y].color, x+1, y-2))
            FeldFig[x+1][y-2].dead = true;
    }
        break;
    case 5:
    {
        if (Intervall(x+1,y) &&  (FeldFig[x+1][y].art == 0))
            FeldFig[x+1][y].possible = true;

        if (Intervall(x-1,y) &&  (FeldFig[x-1][y].art == 0))
            FeldFig[x-1][y].possible = true;

        if (Intervall(x,y+1) &&  (FeldFig[x][y+1].art == 0))
            FeldFig[x][y+1].possible = true;

        if (Intervall(x,y-1) &&  (FeldFig[x][y-1].art == 0))
            FeldFig[x][y-1].possible = true;

        if (Intervall(x+1,y-1) &&  (FeldFig[x+1][y-1].art == 0))
            FeldFig[x+1][y-1].possible = true;

        if (Intervall(x-1,y-1) &&  (FeldFig[x-1][y-1].art == 0))
            FeldFig[x-1][y-1].possible = true;

        if (Intervall(x-1,y+1) &&  (FeldFig[x-1][y+1].art == 0))
            FeldFig[x-1][y+1].possible = true;

        if (Intervall(x+1,y+1) &&  (FeldFig[x+1][y+1].art == 0))
            FeldFig[x+1][y+1].possible = true;

        if(Gegner(FeldFig[x][y].color, x+1, y-1))
            FeldFig[x+1][y-1].dead = true;
        if(Gegner(FeldFig[x][y].color, x-1, y-1))
            FeldFig[x-1][y-1].dead = true;
        if(Gegner(FeldFig[x][y].color, x+1, y+1))
            FeldFig[x+1][y+1].dead = true;
        if(Gegner(FeldFig[x][y].color, x-1, y+1))
            FeldFig[x-1][y+1].dead = true;

        if(Gegner(FeldFig[x][y].color, x+1, y))
            FeldFig[x+1][y].dead = true;
        if(Gegner(FeldFig[x][y].color, x-1, y))
            FeldFig[x-1][y].dead = true;
        if(Gegner(FeldFig[x][y].color, x, y+1))
            FeldFig[x][y+1].dead = true;
        if(Gegner(FeldFig[x][y].color, x, y-1))
            FeldFig[x][y-1].dead = true;
    }
        break; // Koenig
    case 6:
    {
        i = 1;
        while((FeldFig[x+i][y+i].art == 0)&& (Intervall(x+i,y+i)))
        {
            FeldFig[x+i][y+i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x+i, y+i))
        {
            FeldFig[x+i][y+i].dead = true;
        }

        i = 1;
        while((FeldFig[x-i][y+i].art == 0)&& (Intervall(x-i,y+i)))
        {
            FeldFig[x-i][y+i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x-i, y+i))
        {
            FeldFig[x-i][y+i].dead = true;
        }

        i = 1;
        while((FeldFig[x+i][y-i].art == 0)&& (Intervall(x+i,y-i)))
        {
            FeldFig[x+i][y-i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x+i, y-i))
        {
            FeldFig[x+i][y-i].dead = true;
        }
        i = 1;
        while((FeldFig[x-i][y-i].art == 0)&& (Intervall(x-i,y-i)))
        {
            FeldFig[x-i][y-i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x-i, y-i))
        {
            FeldFig[x-i][y-i].dead = true;
        }

        i = 1;
        while(FeldFig[x][y+i].art == 0)
        {
            FeldFig[x][y+i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x, y+i))
        {
            FeldFig[x][y+i].dead = true;
        }

        i = 1;
        while((FeldFig[x+i][y].art == 0)  && (Intervall(x+i,y)))
        {
            FeldFig[x+i][y].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x+i, y))
        {
            FeldFig[x+i][y].dead = true;
        }

        i = 1;
        while(FeldFig[x][y-i].art == 0)
        {
            FeldFig[x][y-i].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x, y-i))
        {
            FeldFig[x][y-i].dead = true;
        }

        i = 1;
        while(FeldFig[x-i][y].art == 0)
        {
            FeldFig[x-i][y].possible = true;
            i++;
        }
        if(Gegner(FeldFig[x][y].color, x-i, y))
        {
            FeldFig[x-i][y].dead = true;
        }

    }
        break; // Dame
    }
}

bool MainWindow::Matt(int color)
{
    bool Erg;
    Erg = false;
    for(int i = 1;i<7;i++) // Figuren
    {
        for(int j = 0; j<8;j++) // startx
        {
            for(int k = 0;k<0;k++) // starty
            {
                if((FeldFig[i][j].art > 0) && FeldFig[i][j].color == color)
                    Zug_meoglich(i,j,k);
                for(int l = 0; l<8;l++) // x
                {
                    for(int m = 0;m<0;m++) // y
                    {
                        if((FeldFig[i][j].possible == true) && (FeldFig[l][m].art > 5))
                        {
                            Erg = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    return Erg;
}

bool MainWindow::KoenigMatt(int color) // Beta, nicht vollständig
{
    bool Erg;
    Erg = false;

    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if((FeldFig[i][j].art == 5) && FeldFig[i][j].color != color)
            {
                if(FeldFig[i][j].dead == 1)
                {
                    Erg = true;
                }
            }
        }
    }

    return Erg;
}

void MainWindow::InfoBox()
{
    QMessageBox msgBox;
    msgBox.setText("Info");
    msgBox.setInformativeText("Ein Programm von:\nChristian Hohlbein\nAlexander Baumstark\n\nUpdates folgen!");
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.setDefaultButton(QMessageBox::Save);
    msgBox.exec();
}

void MainWindow::HelpBox()
{
    QMessageBox msgBox;
    msgBox.setText("Hilfe");
    msgBox.setInformativeText("Machen Sie zuerst einen LINKSKLICK auf die Figur mit der Sie ziehen möchten.\nEs werden ihnen die möglichen Züge in Grün und Rot angezeigt.\nKlicken Sie nun mit einem RECHTSKLICK auf eine mögliche Position um die Figur dahin zu platzieren!");
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.setDefaultButton(QMessageBox::Save);
    msgBox.exec();
}
